(function(){
    _$(document).ready(function(){
        function App(){};
        var page=0;
        var tag=-1;
        App.prototype.ajaxFn=function(){
            _$.post('/getmaleCate',function(data){
                if(data['code']==1){
                    var cg=data['info']['cg'];
                    var subc=data['info']['subc'];
                    var html='<a href="javascript:" class="btn-tag active jsTag" data-value="-1">全站</a>';
                    for(var val in cg){
                        if(val==cg.length-1){
                            break;
                        }
                        html+='<a href="javascript:" class="btn-tag jsTag" data-value="'+cg[val].cgid+'">'+cg[val].type+'</a>';
                    }
                    _$(".toplist-tag").html(html);
                    tagClick();
                }
            });
            function initBook(){
                _$.ajax({
                    type: 'post',
                    url: '/getRankBook',
                    data: {type:type,tag:tag, page: 0},
                    async: false,
                    success: function (json) {
                        var dropload = $('#list').dropload({
                            scrollArea: window,
                            domDown: {
                                domClass: 'dropload-down',
                                domRefresh: '<div class="dropload-refresh">↑上拉加载</div>',
                                domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                                domNoData: '<div class="dropload-noData">已无数据</div>'
                            },
                            //上拉
                            loadDownFn: function (me) {
                                var pageLength = json['count'];
                                if (json['count'] % 10 > 0) {
                                    pageLength = (json['count'] / 10) + 1;
                                }
                                if (page > pageLength) {
                                    me.lock();
                                    me.noData();
                                    me.resetload();
                                } else {
                                    console.log(page);
                                    _$.ajax({
                                        type: 'post',
                                        url: '/getRankBook',
                                        data: {type:type,tag: tag, page: page},
                                        async: false,
                                        success: function (data) {
                                            console.log(data);
                                            if (data.code == 1) {
                                                var info = data['info'];
                                                if (info == "") {
                                                    me.lock();
                                                    me.noData();
                                                    me.resetload();
                                                } else {
                                                    if (info.length > 0) {
                                                        var html = "";
                                                        for (var val in info) {
                                                            info[val]['index'] = parseInt(val) + 1;
                                                            if (info[val]['stateType'] == "T") {
                                                                info[val]['stateType'] = "连载中";
                                                            } else if (info[val]['stateType'] == "F") {
                                                                info[val]['stateType'] = "完结";
                                                            }
                                                            if (info[val]['cnt'] > 10000) {
                                                                info[val]['cnt'] = (info[val]['cnt'] / 10000).toFixed(2) + "万字";
                                                            } else {
                                                                info[val]['cnt'] = info[val]['cnt'] + "字";
                                                            }
                                                            html += template('tpl', info[val]);
                                                        }
                                                        // 为了测试，延迟1秒加载
                                                        setTimeout(function () {
                                                            // 插入加载使用 html() 重置 DOM
                                                            _$("#list .dropload-down").before(html);
                                                            page++;
                                                            // 每次数据加载完，必须重置
                                                            me.resetload();
                                                        }, 1000);
                                                    }
                                                }
                                            }else {
                                                me.lock();
                                                me.noData();
                                                me.resetload();
                                            }
                                        }
                                    });
                                }
                            }
                        })
                    }
                });
            }
            //初始化获取书籍
            initBook();
            function tagClick(){
                _$(".toplist-tag a").click(function(){
                    _$(this).addClass('active').siblings('a').removeClass('active');
                    var value=_$(this).attr('data-value');
                    $('#list').html("");
                    $(window).scrollTop(0);
                    tag=parseInt(value);
                    page=0;
                    initBook();
                });
            }
        };
        App.prototype.eventFn=function(){
            _$("#hbt").html(getQueryString('type'));
        };
        App.prototype.init=function(){
            //事件调用
            this.ajaxFn();
            this.eventFn();
        };
        //程序初始化
        new App().init();
    })
})();